const express = require("express");
const {
  createCategory,
  getAllCategories,
  getCategoryById,
  updateCategory,
  deleteCategory,
  getSubCategories,
  createSubCategory,
  deleteSubCategory,
  updateSubCategory,
  getAllSuCategories,
  getSubCategoryById, // Import the new controller function
} = require("../controllers/categoryController");
const { authMiddleware, hasPermission } = require("../middleware/authMiddleware");

const router = express.Router();

// CREATE Category
router.post(
  "/",
  authMiddleware,
  (req, res, next) => {
    if (req.user.role.toLowerCase() === "admin") return next();
    return hasPermission("Categories", "Add")(req, res, next);
  },
  createCategory
);

// READ ALL Categories
router.get(
  "/",
  authMiddleware,
  (req, res, next) => {
    if (req.user.role.toLowerCase() === "admin") return next();
    return hasPermission("Categories", "View")(req, res, next);
  },
  getAllCategories
);

// READ all SubCategories (if needed)
router.get(
  "/allsucategories",
  authMiddleware,
  (req, res, next) => {
    console.log("✅ Matched /allsucategories route");
    if (req.user.role.toLowerCase() === "admin") return next();
    return hasPermission("Categories", "View")(req, res, next);
  },
  getAllSuCategories
);

// **Place the subcategories routes before the single category route**
// GET SubCategories for a Category
router.get(
  "/:id/subcategories",
  authMiddleware,
  (req, res, next) => {
    if (req.user.role.toLowerCase() === "admin") return next();
    return hasPermission("Categories", "View")(req, res, next);
  },
  getSubCategories
);

// NEW: GET Single SubCategory by ID
router.get(
  "/sub/:id",
  authMiddleware,
  (req, res, next) => {
    if (req.user.role.toLowerCase() === "admin") return next();
    return hasPermission("Categories", "View")(req, res, next);
  },
  getSubCategoryById
);

// READ Single Category
router.get(
  "/:id",
  authMiddleware,
  (req, res, next) => {
    if (req.user.role.toLowerCase() === "admin") return next();
    return hasPermission("Categories", "View")(req, res, next);
  },
  getCategoryById
);

// UPDATE Category
router.put(
  "/:id",
  authMiddleware,
  (req, res, next) => {
    if (req.user.role.toLowerCase() === "admin") return next();
    return hasPermission("Categories", "Edit")(req, res, next);
  },
  updateCategory
);

// DELETE Category
router.delete(
  "/:id",
  authMiddleware,
  (req, res, next) => {
    if (req.user.role.toLowerCase() === "admin") return next();
    return hasPermission("Categories", "Delete")(req, res, next);
  },
  deleteCategory
);

// CREATE SubCategory
router.post(
  "/sub",
  authMiddleware,
  (req, res, next) => {
    if (req.user.role.toLowerCase() === "admin") return next();
    return hasPermission("Categories", "Add")(req, res, next);
  },
  createSubCategory
);

// UPDATE SubCategory
router.put(
  "/sub/:id",
  authMiddleware,
  (req, res, next) => {
    if (req.user.role.toLowerCase() === "admin") return next();
    return hasPermission("Categories", "Edit")(req, res, next);
  },
  updateSubCategory
);

// DELETE SubCategory
router.delete(
  "/sub/:id",
  authMiddleware,
  (req, res, next) => {
    if (req.user.role.toLowerCase() === "admin") return next();
    return hasPermission("Categories", "Delete")(req, res, next);
  },
  deleteSubCategory
);

module.exports = router;
